import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gradient_app_bar/gradient_app_bar.dart';
import 'package:love_app/config/app.dart';
import 'package:love_app/enum/bind_lover.dart';
import 'package:love_app/routes/application.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
import 'package:shared_preferences/shared_preferences.dart';

class Su {
  // 通过flutter_screenutil获取适配的高和宽
  double h(double height) => ScreenUtil().setHeight(height);
  double w(double width) => ScreenUtil().setWidth(width);
  double s(double size) => ScreenUtil().setSp(size);

  // 返回一个简单的字体样式
  TextStyle textStyle(
      Color color, // 字体颜色
      double size, // 字体大小
      {FontWeight fontWeight} // 字体粗细
      ) {
    return TextStyle(
      color: color,
      fontSize: s(size),
      fontWeight: FontWeight.w400 ?? fontWeight,
      decoration: TextDecoration.none, // 去掉字体默认两个下划线
    );
  }

  // 自定义flutter打印函数
  void suPrint(Object data, {String msg}) {
    if (App.showPrint = true) {
      var date = DateTime.now();
      String printTime = date.hour.toString() + ':' + date.minute.toString();
      Map map = {
        'msg': msg ?? '打印变量',
        'time': printTime,
        'data': data,
      };
      print(map);
    } else {}
  }

  // 10位时间戳转时间格式
  String timestampToTime(int timestamp, {String cut = "."}){
    if(timestamp != null){
      DateTime date = DateTime.fromMillisecondsSinceEpoch(timestamp*1000);
      return "${date.year}$cut${date.month}$cut${date.day}";
    }
    return "";
  }

  // 自定义flutter打印函数
  static suPrints(Object data, {String msg}) {
    if (App.showPrint = true) {
      var date = DateTime.now();
      String printTime = date.hour.toString() + ':' + date.minute.toString();
      Map map = {
        'msg': msg ?? '打印变量',
        'time': printTime,
        'data': data,
      };
      print(map);
    } else {}
  }

  // 渐变颜色状态栏
  PreferredSizeWidget appBar(
    String title, {
    TextStyle style, // 导航栏字体样式
    List<Color> colors, // 一组颜色，用于渐变
    String actionText, // 右边的点击字体
    TextStyle actionTextStyle, // 右边的点击字体样式
    Function onActionClick, // 右边的点击字体响应
    // IconData leftLeadingIcon, // 右边的icon图标
    // Color leftLeadingIconColor, // 右边的icon颜色
    // Function onIconButtonClick,  // 右边的icon事件
  }) {
    Widget _actions = Container(
      padding: EdgeInsets.only(
        right: ScreenUtil().setWidth(20),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          InkWell(
            onTap: onActionClick,
            child: Text(
              actionText == null ? '' : actionText,
              style: actionTextStyle == null
                  ? textStyle(Colors.white, 26)
                  : actionTextStyle,
            ),
          ),
        ],
      ),
    );
    Widget _nullWidget = Container();
    return GradientAppBar(
      title: Text(
        title,
        style: style == null ? textStyle(Colors.white, 36) : style,
      ),
      gradient: LinearGradient(
        colors: colors == null
            ? [
                Color.fromRGBO(239, 144, 194, 1),
                Color.fromRGBO(238, 131, 165, 1)
              ]
            : colors,
      ),
      centerTitle: true,
      actions: actionText == null ? <Widget>[_nullWidget] : <Widget>[_actions],
    );
  }

  // 获取app文件目录
  Future getAppFilePath() async {
    Directory dir = await getApplicationDocumentsDirectory();
    return dir.path;
  }

  // 返回带app路径的文件名
  Future getFileName(String fileName) async {
    var appPath = await getAppFilePath();
    return path.join(appPath, fileName);
  }

  //返回一个SharedPreferences对象
  Future<SharedPreferences> presObject() async {
    var pres = await SharedPreferences.getInstance();
    return pres;
  }

  ///根据给定的日期得到format后的日期  ///
  String getDate(String timeStamp) {
    DateTime time =
        DateTime.fromMillisecondsSinceEpoch(int.parse(timeStamp + "000"));
    String timeString = time.toString();
    //现在的日期
    var today = DateTime.now();
    //今天的23:59:59
    var standardDate = DateTime(today.year, today.month, today.day, 23, 59, 59);
    //传入的日期与今天的23:59:59秒进行比较
    Duration diff = standardDate.difference(time);
    if (diff < Duration(days: 1)) {
      //今天
      // 09:20
      return timeString.substring(11, 16);
    } else if (diff >= Duration(days: 1) && diff < Duration(days: 2)) {
      //昨天
      //昨天09:20
      return "昨天 " + timeString.substring(11, 16);
    } else {
      //昨天之前
      // 2019-01-23 09:20
      return timeString.substring(0, 16);
    }
  }

  // 返回阿拉伯数字转中文
  String suNumToString(int intNum) {
    String _data;
    switch (intNum) {
      case 1:
        {
          _data = '一';
        }
        break;
      case 2:
        {
          _data = '二';
        }
        break;
      case 3:
        {
          _data = '三';
        }
        break;
      case 4:
        {
          _data = '四';
        }
        break;
      case 5:
        {
          _data = '五';
        }
        break;
      case 6:
        {
          _data = '六';
        }
        break;
      case 7:
        {
          _data = '七';
        }
        break;
      case 8:
        {
          _data = '八';
        }
        break;
      case 9:
        {
          _data = '九';
        }
        break;
      case 0:
        {
          _data = '零';
        }
        break;
    }
    return _data;
  }

  Future<void> hasLover(
      BuildContext context, BindLover bindLover, String route) {
    if (bindLover == BindLover.hasBind) {
      Application.router.navigateTo(context, route);
      return null;
    } else {
      return showCupertinoDialog(
        context: context,
        builder: (BuildContext context) {
          return CupertinoAlertDialog(
            title: Text("请绑定后再使用!!!"),
            content: Text("立即绑定另一半，开启情侣生活吧"),
            actions: <Widget>[
              CupertinoDialogAction(
                child: Text('立即绑定？'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
              CupertinoDialogAction(
                child: Text('再想想。'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        },
      );
    }
  }
}

// 单例实现方法
// class UserManager {
//   static UserManager _instance;

//   factory UserManager() => _getInstance();

//   static UserManager get instance => _getInstance();

//   static UserManager _getInstance() {
//     if (_instance == null) {
//       _instance = UserManager._();
//     }

//     return _instance;
//   }

//   UserManager._();

//   User _user;

//   User get user {
//     if (_user != null) {
//       return _user;
//     }
//     _user = User.fromJson(SpUtil.getObject(spfKey("user")));
//     return _user;
//   }

//   saveUser(User user) {
//     this._user = user;
//     SpUtil.putObject(spfKey("user"), user);
//   }

//   deleteUser() {
//     this._user = null;
//     SpUtil.remove(spfKey("user"));
//   }
// }
